/**Created by the LayaAirIDE*/
module view.dlg{
	export class Rank extends ui.dlg.RankUI{
		constructor(){
			super();
			this.Init();
		}

		Init() {
			this.btnClose.on(Laya.Event.CLICK, this, this.Close);
			this.btnInvite.on(Laya.Event.CLICK, this, this.shareCommon);
			this.tabRank.selectHandler = new Laya.Handler(this, this.TabSelect);
			this.listScore.renderHandler = new Laya.Handler(this, this.ScoreRankRender);
			this.listWeekScore.renderHandler = new Laya.Handler(this, this.WeekScoreRankRender);
			this.listScore.selectHandler = new Laya.Handler(this, this.ScoreRankSelect);
			this.listWeekScore.selectHandler = new Laya.Handler(this, this.WeekScoreRankSelect);
			this.listScore.visible = true;
			this.listWeekScore.visible = false;
			this.listScore.repeatY = Cfg.RankLimit;
			this.listWeekScore.repeatY = Cfg.RankLimit;
		}

		Close() {
			this.listScore.selectedIndex = -1;
			this.listWeekScore.selectedIndex = -1;
			this.close();
		}

		TabSelect(index:number) {
			if (0 == index) {
				this.listScore.visible = true;
				this.listWeekScore.visible = false;
				this.labWeekScore.visible = false;
			} else {
				this.listScore.visible = false;
				this.listWeekScore.visible = true;
				this.labWeekScore.visible = true;
			}
		}

		ShowRank() {
			this.listScore.array = RankModel.GetScoreRankArray();
			this.listWeekScore.array = RankModel.GetWeekScoreRankArray();
			this.popup();
		}

		ScoreRankRender(cell:Laya.Box, index:number) {
			let friendData = this.listScore.array[index];
			cell.getChildByName("labScore")["text"] = friendData.score.toString();
			cell.getChildByName("labName")["text"] = friendData.nickName;
			if (friendData.avatarUrl.length > 0) {
				(cell.getChildByName("imgHead") as Laya.Image).loadImage(friendData.avatarUrl, 0, 0, 100, 100);
			}
			if (index >= 3) {
				cell.getChildByName("labIndex")["text"] = (index+1).toString();
			} else {
				cell.getChildByName("labIndex")["text"].visible = false;
				(cell.getChildByName("imgHat") as Laya.Image).graphics.clear();
				(cell.getChildByName("imgHat") as Laya.Image).visible = true;
				if (0 == index) {
					(cell.getChildByName("imgHat") as Laya.Image).loadImage("rank/first.png", 0, 0);
				} else if (1 == index) {
					(cell.getChildByName("imgHat") as Laya.Image).loadImage("rank/second.png", 0, 0);
				} else if (2 == index) {
					(cell.getChildByName("imgHat") as Laya.Image).loadImage("rank/third.png", 0, 0);
				}
			}
		}

		WeekScoreRankRender(cell:Laya.Box, index:number) {
			let friendData = this.listWeekScore.array[index];
			cell.getChildByName("labScore")["text"] = friendData.weekScore.toString();
			cell.getChildByName("labName")["text"] = friendData.nickName;
			if (friendData.avatarUrl.length > 0) {
				(cell.getChildByName("imgHead") as Laya.Image).loadImage(friendData.avatarUrl, 0, 0, 100, 100);
			}
			if (index >= 3) {
				cell.getChildByName("labIndex")["text"] = (index+1).toString();
			} else {
				cell.getChildByName("labIndex")["text"].visible = false;
				(cell.getChildByName("imgHat") as Laya.Image).graphics.clear();
				(cell.getChildByName("imgHat") as Laya.Image).visible = true;
				if (0 == index) {
					(cell.getChildByName("imgHat") as Laya.Image).loadImage("rank/first.png", 0, 0);
				} else if (1 == index) {
					(cell.getChildByName("imgHat") as Laya.Image).loadImage("rank/second.png", 0, 0);
				} else if (2 == index) {
					(cell.getChildByName("imgHat") as Laya.Image).loadImage("rank/third.png", 0, 0);
				}
			}
		}

		shareCommon(){
			CtrlManager.GetCtrl(CtrlNames.Share).shareCommon();
		}

		ScoreRankSelect(index:number) {
			if (index >= 0) {
				let friendData = this.listScore.array[index];
				if (friendData.dbid != KBEngine.app.player().dbid) {
					this.Close();
					Game.viewFarm.showMaskCloud();
					ComFun.BaseCall("enterFriendFarm", friendData.dbid);	
				}
			}
		}

		WeekScoreRankSelect(index:number) {
			if (index >= 0) {
				let friendData = this.listScore.array[index];
				if (friendData.dbid != KBEngine.app.player().dbid) {
					this.Close();
					Game.viewFarm.showMaskCloud();
					ComFun.BaseCall("enterFriendFarm", friendData.dbid);
				}
			}
		}
	}
}